//
//  ExampleViewController.m
//  NekaPrice
//
//  Created by yu on 2018/6/22.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "ExampleViewController.h"
#import "ExampleSearchView.h"
#import "ExampleCell.h"
#import "ExampleViewModel.h"
#import "ExampleDetailViewController.h"
#import "ExampleSearchViewController.h"
@interface ExampleViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)ExampleSearchView *searchView;
@property (nonatomic, strong)ExampleViewModel *exampleVM;
@property (nonatomic, strong)MJRefreshNormalHeader *header;
@property (nonatomic, strong)MJRefreshAutoNormalFooter *footer;

@end

@implementation ExampleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self hideNavBar];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:NO];
    
    
    [self.contentView addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.top);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.searchView.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-self.bottom);
    }];
    
    [self showHUD];
    [self.exampleVM requestExampleList:^{
        [self hideHUD];
        [self.searchView setFirstList:self.exampleVM.categoryList];
        [self addHeaderAndFooter];
        [self.collectionView reloadData];
    }];
    self.searchView.searchText.text = @"";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkAvailableAction) name:@"NETWORK_AVAILABLE" object:nil];
    
    
}

- (void)netWorkAvailableAction {
    
    if (self.exampleVM.showExampleList.count == 0) {
        [self showHUD];
        [self.exampleVM requestExampleList:^{
            [self hideHUD];
            [self.collectionView reloadData];
        }];
    }
    
}


- (ExampleViewModel *)exampleVM {
    if (_exampleVM == nil) {
        _exampleVM = [[ExampleViewModel alloc] init];
    }
    return _exampleVM;
}

//http://www.neka.com.cn/plus/caselist.php
//http://www.neka.com.cn/plus/caselist.php?tid=114

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.itemSize = CGSizeMake(kExampleCellWidth, kExampleCellWidth * 0.6 + 40);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, K_WIDTH, K_HEIGHT) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.alwaysBounceVertical = YES;
        [_collectionView registerClass:[ExampleCell class] forCellWithReuseIdentifier:ExampleCellID];
        
        
        
    }
    return _collectionView;
}

- (void)addHeaderAndFooter {
    if (_header == nil || _footer == nil) {
        _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction:)];
        self.collectionView.mj_header = _header;
        
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction:)];
        self.collectionView.mj_footer = _footer;
    }
}

- (void)headerAction:(MJRefreshNormalHeader *)header {
    [header setState:MJRefreshStateIdle];
    [self showHUD];
    [self.exampleVM requestExampleList:^{
        [self hideHUD];
        [header endRefreshing];
        [self.collectionView reloadData];
    }];
    
}

- (void)footerAction:(MJRefreshAutoNormalFooter *)footer {
    [self showHUD];
    [self.exampleVM makeShowExampleList:^(BOOL canLoad) {
        [self hideHUD];
        [footer endRefreshing];
        if (canLoad) {
            [self.collectionView reloadData];
        }else {
            [MBProgressHUD showError:@"到底了" toView:self.view];
        }
    }];
    
}


- (ExampleSearchView *)searchView {
    if (_searchView == nil) {
        _searchView = [[ExampleSearchView alloc] initAndSearchTextInCenter:NO];
        __weak typeof(self) weakSelf = self;
        _searchView.selectedBlock = ^(NSString *url) {
            [weakSelf.view endEditing:YES];
            [weakSelf.exampleVM requestExampleListByURL:url withBlock:^{
                [weakSelf.collectionView setContentOffset:CGPointMake(0, 0)];
                [weakSelf.collectionView reloadData];
            }];
        };
        
        
        _searchView.searchBlock = ^(NSString *keywords) {
            NSLog(@"%@", keywords);
            [weakSelf.view endEditing:YES];
            if (keywords.length == 0) {
                [MBProgressHUD showError:@"请输入搜索内容"];
            }else {
                ExampleSearchViewController *searchVC = [[ExampleSearchViewController alloc] init];
                searchVC.keywords = keywords;
                searchVC.dataSource = weakSelf.exampleVM.allExampleList;
                [weakSelf.navigationController pushViewController:searchVC animated:YES];
            }
            
        };
        
    }
    return _searchView;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.exampleVM.showExampleList.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ExampleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ExampleCellID forIndexPath:indexPath];
    ExampleModel *model = self.exampleVM.showExampleList[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    ExampleDetailViewController *detail = [[ExampleDetailViewController alloc] init];
    ExampleModel *model = self.exampleVM.showExampleList[indexPath.row];
    detail.exampleModel = model;
    [self.navigationController pushViewController:detail animated:YES];
    
}


//滚动/点击页面时,隐藏选择分类弹框
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
    [self.searchView hideAllViews];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    [self.searchView hideAllViews];
    
}





@end
